<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11
 * Time: 11:14
 */

namespace App\LogicService;

use App\LogicService\Contracts\MedicalRecordLogicContract;
use App\Models\MedicalRecord;


class MedicalRecordLogic extends BaseLogic implements MedicalRecordLogicContract
{

    public function getLastRecord($record_id, $type, $patient_id)
    {
        if ($type == 1) {
            $record = MedicalRecord::find($record_id);

            $last_record = MedicalRecord::where('patient_id', $record->patient_id)
                ->where('is_del',0)
                ->where('id', '<>', $record_id)
                ->orderBy('created_at', 'desc')
                ->first();

            return $last_record;
        } else {
            $last_record = MedicalRecord::where('patient_id', $patient_id)
                ->where('is_del',0)
                ->orderBy('created_at', 'desc')
                ->first();

            return $last_record;
        }

    }

    public function getList($patient_id)
    {

        $records = MedicalRecord::where('patient_id', $patient_id)
            ->where('is_del',0)
            ->orderBy('created_at', 'desc')
            ->get()
            ->toArray();
        return $records;
    }


    public function getListByHospitalId($patient_id, $hospital_id)
    {

        $records = MedicalRecord::where('patient_id', $patient_id)
            ->where('hospital_id',$hospital_id)
            ->where('is_del',0)
            ->orderBy('created_at', 'desc')
            ->get()
            ->toArray();

        return $records;
    }
}
